<?php
require_once 'App/ImageService.php';
require_once 'Zend/Controller/Action.php';

class ImageController extends Zend_Controller_Action
{
	protected $imageService;
	
	public function preDispatch()
	{
		$this->session = new Zend_Session_Namespace('default');
		$this->imageService = new App_ImageService();
		$layout = Zend_Layout::getMvcInstance();
		$layout->message = '';
	}
	
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
		
    }
    
	public function addAction()
	{
		$form = new App_Forms_ImageForm();
		
		$filters = array('imgFile' => 'HtmlEntities');
		$validators = array('imgFile' => new Zend_Validate_NotEmpty());
		
		
		if($this->_request->isPost()) {
			$values = $form->getValues();
			$otherData = $this->getRequest()->getPost();
			$upload = new Zend_File_Transfer_Adapter_Http();
			$upload->setDestination('..\user_data');
			$upload->addFilter('HtmlEntities');
			$fileName =  $upload->getFileName('imgFile', false);
			
			try{
				$upload->receive();
			}
			catch(Zend_File_Transfer_Exception $e)
			{
				throw new Exception('Bad image data: ' . $e->getMessage());
			}
			
			$this->imageService->saveImage($otherData['myAlbum'], $upload->getFileName('imgFile'));
			$this->session->viewAlbumID = $otherData['myAlbum'];	
			$this->_redirect('album');
			$this->_helper->getViewRenderer->setNoRender(true);
		}
		
		$this->view->form = $form;
	}
}

